﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using TierApplication;
using System.Text;
using System.Data;

public partial class ForeProgram_shopOrderCenter : System.Web.UI.Page
{
    #region Page_Load
    /// <summary>
    /// Page_Load
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindTable();
        }
    }
    #endregion

    #region 绑定 GridView
    /// <summary>
    /// 绑定 GridView
    /// </summary>
    private void BindTable()
    {
        try
        {
            T_ORDER order = new T_ORDER();
            StringBuilder sb = new StringBuilder();
            sb.Append(" And accountid='" + Security.GetAccountId() + "'");
            if (txtorderNo.Text.Trim().Length != 0)
            {
                sb.Append(" And orderid='" + txtorderNo.Text + "'");
            }
            if (txtProductName.Text.Trim().Length != 0)
            {
                sb.Append(" And orderid in(select Distinct orderid from t_orderdetail t,t_product b Where t.productid=b.productid And Orderid Is Not Null and productname like '%" + txtProductName.Text + "%') ");
            }
            if (txtproductNo.Text.Trim().Length != 0)
            {
                sb.Append(" And orderid in(select Distinct orderid from t_orderdetail t,t_product b Where t.productid=b.productid And Orderid Is Not Null and productid like '%" + txtproductNo.Text + "%') ");
            }
            if (ViewState["para"] != null)
            {
                if (ViewState["para"].ToString().Equals("New"))
                {
                    sb.Append(" And orderstatus='0'");
                }
                if (ViewState["para"].ToString().Equals("Cancel"))
                {
                    sb.Append(" And orderstatus='2'");
                }
            }
            DataSet ds = order.GetOrderForAccount(sb.ToString());
            int iCount = ds.Tables[0].Rows.Count;
            PagedDataSource pds = new PagedDataSource();
            AspNetPager1.RecordCount = iCount;
            pds.DataSource = ds.Tables[0].AsDataView();
            pds.AllowPaging = true;
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            pds.PageSize = AspNetPager1.PageSize;

            if (iCount > AspNetPager1.PageSize)
            {
                AspNetPager1.Visible = true;
            }
            else
            {
                AspNetPager1.Visible = false;
            }
            gvTable.DataSource = pds;
            gvTable.DataBind();
        }
        catch (Exception ex)
        {
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('绑定数据时出错！');</script>");
            return;
        }
        this.AspNetPager1.CustomInfoHTML = string.Format("共 <font color='red'>{0}</font> 条记录 <font color='red'>{1}/{2}</font>", new object[] { AspNetPager1.RecordCount, AspNetPager1.CurrentPageIndex, AspNetPager1.PageCount });
    }
    #endregion

    #region 事件
    /// <summary>
    /// 分页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindTable();
    }
    protected void gvTable_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.DataItemIndex == -1) return;
        e.Row.Cells[3].Text = "在线支付";
        LinkButton lbView = (LinkButton)e.Row.FindControl("lbView");
        LinkButton lbCancel = (LinkButton)e.Row.FindControl("lbCancel");
        LinkButton lbBank = (LinkButton)e.Row.FindControl("lbBank");
        lbView.CommandArgument = e.Row.RowIndex.ToString();
        lbCancel.CommandArgument = e.Row.RowIndex.ToString();
        lbBank.CommandArgument = e.Row.RowIndex.ToString();
        if (DataBinder.Eval(e.Row.DataItem, "Orderstatus").ToString().Equals("0"))
        {
            lbCancel.Visible = true;
            lbBank.Visible = true;
        }
        else
        {
            lbCancel.Visible = false;
            lbBank.Visible = false;
        }
    }
    protected void gvTable_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument);
        switch (e.CommandName)
        {
            case "View":
                Context.Items.Add("OrderId", gvTable.DataKeys[index].Values["orderid"].ToString());
                Server.Transfer("shopOrderView.aspx");
                break;
            case "Canc":
                T_ORDER order = new T_ORDER();
                int ai_result = -1;
                string ac_orderid = string.Empty;
                string ac_result = string.Empty;
                try
                {
                    order.CancelOrder(gvTable.DataKeys[index].Values["orderid"].ToString(), ref ai_result, ref ac_result);
                    if (ai_result == 0)
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), DateTime.Now.ToLongTimeString(), "<script>alert('取消交易成功！');</script>");
                    }
                    else
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), DateTime.Now.ToLongTimeString(), "<script>alert('" + ac_result + "！');</script>");
                    }
                }
                catch (Exception ex)
                {
                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), DateTime.Now.ToLongTimeString(), "<script>alert('取消交易失败！');</script>");
                }
                BindTable();
                break;
        }
    }
    protected void lbOrder_Click(object sender, EventArgs e)
    {
        ClearCss();
        allOrder.Attributes.Add("class", "bton");
        if (ViewState["para"] != null)
            ViewState.Remove("para");
        BindTable();
    }
    protected void lbNew_Click(object sender, EventArgs e)
    {
        ClearCss();
        newOrder.Attributes.Add("class", "bton");
        ViewState["para"] = "New";
        BindTable();

    }
    protected void lbCancel_Click(object sender, EventArgs e)
    {
        ClearCss();
        cancelOrder.Attributes.Add("class", "bton");
        ViewState["para"] = "Cancel";
        BindTable();
    }

    private void ClearCss()
    {
        allOrder.Attributes.Add("class", "");
        newOrder.Attributes.Add("class", "");
        cancelOrder.Attributes.Add("class", "");
    }
   
    protected void lbSearch_Click(object sender, EventArgs e)
    {
        lbOrder_Click(sender, e);
    }
    #endregion
}